package com.atguigu.gulimall.ware.controller;

import com.atguigu.common.exception.NoWareStockException;
import com.atguigu.common.to.SkuStockTo;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.WareSkuLockTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品库存
 *
 * @author lxx
 * @email 1914264274@qq.com
 * @date 2022-03-08 15:42:01
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    //锁定库存
    @RequestMapping("/lockStock")
    public R lockStock(@RequestBody WareSkuLockTo wareSkuLockTo) {

        try {
            wareSkuService.lockStock(wareSkuLockTo);
            return R.ok();
        } catch (NoWareStockException exception) {
            return R.error();
        }
//        R r = new R();
//        r.setData(lockStatusVo);
//        return r;
    }


    //批量查询商品是否有库存
    @RequestMapping("/hashStock")
    public R hashStock(@RequestBody List<Long> skuIds) {

        List<SkuStockTo> stockToList = wareSkuService.hashStock(skuIds);
        R ok = R.ok();
        ok.setData(stockToList);
        return ok;
//        return ok;
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //支持条件查询功能


//        PageUtils page = wareSkuService.queryPage(params);
        PageUtils page = wareSkuService.queryByCondition(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
